<?php

class MActiveForm extends CActiveForm
{
	public $title;
	
	public function init()
	{
		if(!isset($this->htmlOptions['id']))
			$this->htmlOptions['id']=$this->id;
		else
			$this->id=$this->htmlOptions['id'];
		
		if($this->stateful)
			echo CHtml::statefulForm($this->action, $this->method, $this->htmlOptions);
		else {
			echo CHtml::beginForm($this->action, $this->method, $this->htmlOptions);
		}
		
		echo CHtml::openTag('fieldset');
		if(isset($this->title)) {
			echo CHtml::tag('legend', array(), $this->title, true);
		}
	}
	
	public function run()
	{
		if(is_array($this->focus))
			$this->focus="#".CHtml::activeId($this->focus[0],$this->focus[1]);
	
		echo CHtml::closeTag('fieldset');
		echo CHtml::endForm();
		$cs=Yii::app()->clientScript;
		if(!$this->enableAjaxValidation && !$this->enableClientValidation || empty($this->attributes))
		{
			if($this->focus!==null)
			{
				$cs->registerCoreScript('jquery');
				$cs->registerScript('CActiveForm#focus',"
					if(!window.location.hash)
						jQuery('".$this->focus."').focus();
				");
			}
			return;
		}
	
		$options=$this->clientOptions;
		if(isset($this->clientOptions['validationUrl']) && is_array($this->clientOptions['validationUrl']))
			$options['validationUrl']=CHtml::normalizeUrl($this->clientOptions['validationUrl']);
	
		$options['attributes']=array_values($this->attributes);
	
		if($this->summaryID!==null)
			$options['summaryID']=$this->summaryID;
	
		if($this->focus!==null)
			$options['focus']=$this->focus;
	
		$options=CJavaScript::encode($options);
		$cs->registerCoreScript('yiiactiveform');
		$id=$this->id;
		$cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiactiveform($options);");
	}
	
	public function _getInputLabelId($attribute) {
		return $this->id.'_'.$attribute.'_lbl';
	}
	public function _getInputId($attribute) {
		return $this->id.'_'.$attribute;
	}
	public function _getInputCtrlGrpId($attribute) {
		return $this->id.'_'.$attribute.'_ctrlgrp';
	}
	public function _getInputHelpId($attribute) {
		return $this->id.'_'.$attribute.'_help';
	}
	
	
	public function mActiveCtrlGrpText($model, $attribute, $label = false, $validate_type = false, $htmlOptions = array()) {
		echo CHtml::openTag('div', array('id'=>$this->_getInputCtrlGrpId($attribute), 'class'=>'control-group'));
		echo CHtml::tag('label', array('class'=>'control-label', 
				'id'=>$this->_getInputLabelId($attribute), 
				'for'=>$this->_getInputId($attribute)), 
				$label);
		echo CHtml::openTag('div', array('class'=>'controls'));
		if($validate_type) {
			$htmlOptions['data-validate-type'] = $validate_type;
		}
		echo CHtml::activeTextField($model, $attribute, $htmlOptions);
		echo CHtml::tag('span', array('class'=>'help-inline', 'id'=>$this->_getInputHelpId($attribute)));
		echo CHtml::closeTag('div');
		echo CHtml::closeTag('div');
	}
	
	public function mActiveCtrlGrpPassword($model, $attribute, $label = false, $validate_type = false, $htmlOptions = array()) {
		echo CHtml::openTag('div', array('id'=>$this->_getInputCtrlGrpId($attribute), 'class'=>'control-group'));
		echo CHtml::tag('label', array('class'=>'control-label',
				'id'=>$this->_getInputLabelId($attribute),
				'for'=>$this->_getInputId($attribute)),
				$label);
		echo CHtml::openTag('div', array('class'=>'controls'));
		if($validate_type) {
			$htmlOptions['data-validate-type'] = $validate_type;
		}
		echo CHtml::activePasswordField($model, $attribute, $htmlOptions);
		echo CHtml::tag('span', array('class'=>'help-inline', 'id'=>$this->_getInputHelpId($attribute)));
		echo CHtml::closeTag('div');
		echo CHtml::closeTag('div');
	}
	
	public function mActiveCtrlGrpCheckbox($model, $attribute, $label = false, $validate_type = false, $htmlOptions = array()) {
		
	}
}